/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.core.fadingmessage.calibration.visual;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;

public class PulseColorContainer {
	
	private enum Pulse {PULSE_UP, PULSE_DOWN, NO_PULSE}
	
	private static final float ALPHA_MAX = .85f;

	private final static Color[] colorPulseArray = new Color[10];
	
	private int colorPulseIndex;
	
	private boolean finished;

	private AlphaComposite baseAlphaComposite;
	
	private final static AlphaComposite[] alphaArray = new AlphaComposite[12];

	private int fadingIndex;
	
	private Pulse currentColorPulseMode;
	
	public PulseColorContainer(){
		
		colorPulseIndex = 0;
		for (int i = 0; i < colorPulseArray.length; i++) {
			int c = i * 4;
			colorPulseArray[i] = new Color( 200 + c, 0, 0 + c );
		}
		currentColorPulseMode = Pulse.PULSE_UP;
		
		fadingIndex = alphaArray.length -1;
		baseAlphaComposite = AlphaComposite.getInstance(
				AlphaComposite.SRC_OVER, ALPHA_MAX);
		
		float factor = ALPHA_MAX / (alphaArray.length);
        for (int i = 0; i < alphaArray.length; i++) {
        	float value = (i + 1) * factor;
        	alphaArray[i] = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, value);
		}
		
		finished = false;
	}
	
	
	public Composite requestAlphaComposite( boolean isCalibtrated ){
        
		Composite composite;
		if ( isCalibtrated ) {
			composite = alphaArray[ fadingIndex ];
            if (fadingIndex > 0) {
            	fadingIndex --;
            }
            else{
            	finished = true;
            }
        }
		else {
			composite = baseAlphaComposite;
		}
        
        return composite;
	}
	
	public Color requestColor(boolean mousIsOver){
		
		switch ( currentColorPulseMode ) {
			case NO_PULSE: {
				if ( ! mousIsOver ){
					colorPulseIndex = 1;
					currentColorPulseMode = Pulse.PULSE_UP;
				}
				break;
			}
			case PULSE_UP: {
				if ( mousIsOver ){
					colorPulseIndex--;
					currentColorPulseMode = Pulse.PULSE_DOWN;
				}
				else { // mouse is not over
					colorPulseIndex++;
					boolean maxReached = 
						( colorPulseIndex == colorPulseArray.length );
					if ( maxReached ){
						colorPulseIndex = colorPulseArray.length - 2;
						currentColorPulseMode = Pulse.PULSE_DOWN;
					}					
				}
				break;
			}
			case PULSE_DOWN: {
				colorPulseIndex--;
				boolean minReached = ( colorPulseIndex == -1 );
				if ( ! mousIsOver ){
					if ( minReached ){
						colorPulseIndex = 1;
						currentColorPulseMode = Pulse.PULSE_UP;
					}
				}
				else {  // mouse is not over
					if ( minReached ){
						colorPulseIndex = 0;
						currentColorPulseMode = Pulse.NO_PULSE;
					}
				}
				break;
			}			
		}
		
		Color color = colorPulseArray[colorPulseIndex];
		return color;
	}


	public boolean isFinished() {
		return finished;
	}
	

}
